﻿using System;
using System.Collections.Generic;
using System.Linq;
//using System.Threading;
using System.Web;
using DrillOnline.Hubs;
using Microsoft.AspNet.SignalR;
using DrillOnline.Services.Scenarios;
using DrillOnline.Services;

namespace DrillOnline.Hubs
{

    public class DrillHubUtility
    {
        private readonly IHubContext _hubContext;
        public DrillHubUtility()
        {
            _hubContext = GlobalHost.ConnectionManager.GetHubContext<DrillEventHub>();
        }

        public void BroadcastUpdatedScenes(ActorGroup g, IList<DrillScene> sl, DrillScenarios ds)
        {
            _hubContext.Clients.Group(g.GroupKey).Broadcast(new
            {
                SceneList = sl.Select(v => new
                {
                    ID = v.Model.ID.ToString("N"),
                    Offset = v.Offset,
                    Title = v.Model.Title,
                    Content = v.Model.Content,
                    InfoLocation = v.Model.InfoLocation,
                    ALINK = v.Model.AttachmentKey,
                    ATYPE = v.Model.AttachmentType,
                    Groups = string.Join(",", v.Groups),
                    Type = v.Type,
                    EventType = v.Model.EventType,
                    InjectType = v.Model.InectType,
                    Source = v.Model.Source,
                    SortIndex = v.Model.SortIndex,
                    Flag = v.Model.FlagChar,
                    BT = ds.Timeline.StoryBaseTime.Date.AddSeconds(v.Model.StorySec.Value).ToString("HH点mm分")
                }).ToArray(),
                PS = ds.Timeline.PlayingStartTime,
                SB = ds.Timeline.StoryBaseTime,
                SBT = ds.Timeline.StoryBaseTime,
                PO = ds.Timeline.PlayingOffset,
                TR = ds.Timeline.Config_PlayingTimeRate,
                PI = ds.PlayingSceneIndex
            });
        }

        public void NotifySettingChange(DrillScenarios ds, string userid = null)
        {
            var target = string.IsNullOrEmpty(userid) ? _hubContext.Clients.All : _hubContext.Clients.User(userid);

            target.UpdateSettings(ds.Timeline.PlayingStartTime, ds.Timeline.StoryBaseTime, ds.Timeline.Config_PlayingTimeRate, ds.Timeline.RealCurrentTime, ds.Timeline.StoryCurrentTime);
        }
        public void NotifyStatusChange(DrillScenarios ds, string[] targetGroups = null)
        {

            var target = (null == targetGroups || targetGroups.Length == 0) ?
                _hubContext.Clients.All : _hubContext.Clients.Groups(targetGroups);
            target.StatusChange(DateTime.Now, ds.IsReady, ds.Timeline.Status);
        }
        public void BroadcastMessage(DrillScenarios ds, string fromuser, string flag, string fromuser_group, string[] targetGroups, string message, string eventtype, string targetEventIDN, Microsoft.AspNet.SignalR.Hubs.HubCallerContext context = null)
        {
            BroadcastMessage(ds, fromuser, flag, fromuser_group, targetGroups, string.Empty, message, eventtype, targetEventIDN, context);
        }
        public void BroadcastMessage(DrillScenarios ds, string fromuser, string flag, string fromuser_group, string[] targetGroups, string title, string message, string msgtype, string targetEventIDN, Microsoft.AspNet.SignalR.Hubs.HubCallerContext context = null)
        {
            //if (string.IsNullOrEmpty(fromuser)) fromuser = ContextBoundObject.User.Identity.Name;

            if (null != targetGroups)
            {
                if (!targetGroups.Contains(fromuser_group))
                {
                    targetGroups = targetGroups.Union(new string[] { fromuser_group }).ToArray();
                }
            }
            var target = (null == targetGroups || targetGroups.Length == 0) ?
                _hubContext.Clients.All : _hubContext.Clients.Groups(targetGroups);

            target.PushMessage(DateTime.Now,
                    fromuser,
                    fromuser_group,
                    flag,
                    ds.Timeline.StoryCurrentTime,
                    message, msgtype, targetEventIDN);

            //if (null != context)
            //{
                //_hubContext.Clients.User(context.ConnectionId).PushMessage(DateTime.Now,
                //fromuser,
                //fromuser_group,
                //flag,
                //ds.Timeline.StoryCurrentTime,
                //message, msgtype, targetEventIDN);
            //}
        }


        internal void NotifyPlayIndexChange(int oldidx, DrillScenarios drillScenarios)
        {
            try
            {
                var target = _hubContext.Clients.All;
                var scen = drillScenarios.SceneList[drillScenarios.PlayingSceneIndex];
                var st = drillScenarios.Timeline.StoryBaseTime.Date.AddSeconds(scen.Model.StorySec.Value);
                var et = (st - drillScenarios.Timeline.StoryBaseTime).ToString("d\\天h\\小\\时m\\分s\\秒");
                target.PlayIndexChange(DateTime.Now, st, drillScenarios.Timeline.PlayingOffset, scen.Model.SortIndex, scen.Offset, st.ToString("yyyy年MM月dd日HH点mm分"), DateTime.Now.ToString("yyyy年MM月dd日HH点mm分"),et);
            }
            catch { }
        }


        internal void QueryOldEvents(DrillScenarios ds, string u, string g)
        {

            var sl = ds.GetOldSceneList(g, 0, 999);
            var ga = ds.ActorGroups.First(v => v.GroupKey == g);
            BroadcastUpdatedScenes(ga, sl, ds);
        }
    }


}